package com.chinaedu.oc.action.managegroup;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chinaedu.oc.dao.managegroup.ManageGroupDao;
import com.chinaedu.oc.entity.managegroup.ManageGroup;
import com.chinaedu.oc.util.common.IdCreater;
import com.chinaedu.oc.utils.ProcessResult;
import com.google.common.collect.Lists;


/**
 * @author lidan101
 * 角色列表页
 */
@Controller
@RequestMapping("/group")
public class ManageGroupAction {
	
	@Resource
	private ManageGroupDao manageGroupDao;
	
	/**
	 * 权限列表页集合
	 * @return
	 */
	@RequestMapping("list")
	@ResponseBody
	public ProcessResult list(HttpServletRequest request, Model model) {
		ProcessResult pr= new ProcessResult();
		List<ManageGroup> groups = Lists.newArrayList();
		
		Boolean status =false;
		try {
			groups = manageGroupDao.getGroupsList();
			status=true;
		} catch (Exception e) {
			status=false;
		}
		pr.setData(groups);
		pr.setStatus(status);
		return pr;
	}
	
	
	/**
	 * 角色下拉框数据(集合里套map)
	 * @return
	 */
	@RequestMapping("selectList")
	@ResponseBody
	public  Map<String, Object> selectList() {
		Map<String, Object> result = new HashMap<>();
		List<Object> arrayList = new ArrayList<>();
		List<ManageGroup>  list =  manageGroupDao.getGroupsList();
			for (ManageGroup m : list) {
				Map<String, Object> map = new HashMap<>();
				map.put("key", m.getId());
				map.put("value", m.getGroupName());
				arrayList.add(map);
			}
			result.put("status", true);
			result.put("data", arrayList);
		return result;
	}
	
	
	/**
	 * 新建、编辑角色
	 * @param request
	 * @param group
	 * @return
	 */
	@RequestMapping("saveGroup")
	@ResponseBody
	public ProcessResult saveGroup(HttpServletRequest request,
								@ModelAttribute ManageGroup group
								//@RequestBody ManageGroup group
								) {
		ProcessResult pr= new ProcessResult();
		if(group.getGroupName()==null){
			pr.setStatus(false);
			pr.setMsg("角色名必须输入！");
			return pr;
		}
		if(null==group.getId()||"".equals(group.getId())){
			//新建
			ManageGroup newGroup = new ManageGroup();
			newGroup.setId(IdCreater.getId());
			newGroup.setGroupName(group.getGroupName());
			newGroup.setDescription(group.getDescription());
			newGroup.setAuthValue(group.getAuthValue());
			manageGroupDao.saveGroup(newGroup);
		}else{
			//更新
			ManageGroup existGroup = manageGroupDao.findGroupById(group.getId());
			existGroup.setGroupName(group.getGroupName());
			existGroup.setDescription(group.getDescription());
			existGroup.setAuthValue(group.getAuthValue());
			manageGroupDao.updateGroup(existGroup);
		}
		pr.setStatus(true);
		return pr;
	}
	
}
